package cn.yangyumanage.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import cn.yangyumanage.R;
import cn.yangyumanage.adapter.LeaveMessageWithUserInfoRvAdapter;
import cn.yangyumanage.application.DataApplication;
import cn.yangyumanage.bean.LeaveMessageWithUserInfo;
import cn.yangyumanage.bean.Page;
import cn.yangyumanage.callback.DataCallBack;
import cn.yangyumanage.http.HttpClient;
import cn.yangyumanage.model.LeaveMessageHttpModel;
import cn.yangyumanage.util.ToastUtil;


public class NotPassLeaveMessageFragment extends Fragment {

    private View view;
    private final int state = 2;
    private int currentPage = 1;
    private final int pageSize = 10;
    private final LeaveMessageHttpModel httpModel = new LeaveMessageHttpModel();
    private RecyclerView rv_not_pass_leave_message;
    private LeaveMessageWithUserInfoRvAdapter adapter;
    private SmartRefreshLayout srl_not_pass_leave_message;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_not_pass_leave_message, container, false);
        initView();
        initData();
        return view;
    }

    @Override
    public void onResume() {
        if (!DataApplication.notPassLeaveMessageFlag){
            rv_not_pass_leave_message.scrollToPosition(0);
            srl_not_pass_leave_message.autoRefresh();
            DataApplication.notPassLeaveMessageFlag=true;
        }
        super.onResume();
    }

    public void initView() {
        rv_not_pass_leave_message = view.findViewById(R.id.rv_not_pass_leave_message);
        srl_not_pass_leave_message = view.findViewById(R.id.srl_not_pass_leave_message);
        srl_not_pass_leave_message.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                getLeaveMessage(state, currentPage, pageSize,"refresh");
            }
        });
        srl_not_pass_leave_message.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                currentPage++;
                getLeaveMessage(state, currentPage, pageSize,"loadMore");
            }
        });
    }

    public void initData() {
        initAdapter();
        getLeaveMessage(state, currentPage, pageSize,"initData");
    }

    private void initAdapter() {
        adapter = new LeaveMessageWithUserInfoRvAdapter();
        adapter.setContext(getContext());
        rv_not_pass_leave_message.setAdapter(adapter);
    }

    //根据留言状态分页查询留言及其用户信息
    public void getLeaveMessage(int state, int currentPage, int pageSize,String action) {
        httpModel.getLeaveMessage(state, currentPage, pageSize, new DataCallBack() {
            @Override
            public void getData(Object data) {
                Page<LeaveMessageWithUserInfo> page = (Page) data;
                List<LeaveMessageWithUserInfo> records = page.getRecords();
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        switch (action) {
                            case "initData":
                                adapter.setData(records);
                                break;
                            case "loadMore":
                                if (records.size() == 0) {
                                    ToastUtil.show(getContext(), "没有更多数据了");
                                    NotPassLeaveMessageFragment.this.currentPage--;
                                }
                                adapter.setData(records);
                                srl_not_pass_leave_message.finishLoadMore();
                                break;
                            case "refresh":
                                adapter.clearAllData();
                                adapter.setData(records);
                                srl_not_pass_leave_message.finishRefresh();
                                break;
                        }
                    }
                });
            }
        });
    }
}